package zjitc.utils;

import java.util.Map;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/12 14:25
 * @description:
 */
public class RedisConfig  {
  private JedisPool pool;
  public RedisConfig(JedisPool jedisPool)
  {
    this.pool=jedisPool;
  }
  public void hset(String key,String field,String value)
  {
    Jedis jedis=null;
    try{
      jedis=pool.getResource();
      jedis.hset(key,field,value);
    }
    finally {
      if(jedis!=null)
      {
       this.pool.returnResource(jedis);
      }
    }
  }
  public void  set(String key,String value)
  {
    Jedis jedis=null;
    try{
      jedis=pool.getResource();
      jedis.set(key,value);
    }
    finally {
      if(jedis!=null)
      {
        this.pool.returnResource(jedis);
      }
    }
  }
  public void hmset(String var1, Map<String, String> var2)
  {
    Jedis jedis=null;
    try{
      jedis=pool.getResource();
      jedis.hmset(var1,var2);
    }
    finally {
      if(jedis!=null)
      {
        this.pool.returnResource(jedis);
      }
    }
  }
  public void sadd(String key,String...members)
  {
    Jedis jedis=null;
    try{
     jedis= pool.getResource();
     jedis.sadd(key,members);
    }
    finally {
      if(jedis!=null)
      {
        this.pool.returnResource(jedis);
      }
    }
  }
  public Set<String> smembers(String key)
  {
    Jedis jedis=null;
    try{
      jedis= pool.getResource();
    }
    finally {
      if(jedis!=null)
      {
        this.pool.returnResource(jedis);
      }
    }
    return jedis.smembers(key);
  }
  public Boolean exists(String key)
  {
    Jedis jedis=null;
    try{
      jedis=pool.getResource();
    }
    finally {
      if(jedis!=null)
      {
        this.pool.returnResource(jedis);
      }
    }
    return  jedis.exists(key);
  }
  public Boolean sismember(String key, String member)
  {
    Jedis jedis=null;
    try {
      jedis=pool.getResource();
    }
    finally {
      if(jedis!=null)
      {
        this.pool.returnResource(jedis);
      }
    }
   return jedis.sismember(key, member);
  }

  public Long srem(String key, String... members)
  {
    Jedis jedis=null;
    try {
      jedis=pool.getResource();
    }
    finally {
      if(jedis!=null)
      {
        this.pool.returnResource(jedis);
      }
    }
    return jedis.srem(key,members);
  }

}
